package com.hxd.otc.widget;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.dialog.BasePopupWindow;

/**
 * 作  者 :  卫广玉
 * 时  间 :  2018/6/6 18:30
 * 描  述 :  类型选择pop
 */
public class TypeWindow extends BasePopupWindow<TypeWindow> {

    private String[] mTypes;
    private String mCurrentType;
    private OnItemClickListener mOnItemClickListener;
    private ListView mListview;
    private int mWidth;
    private int layoutId;
    private ListAdapter adapter;

    public void setTypes(String[] types) {
        this.mTypes = types;
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    public TypeWindow(Context context, String[] types, String type, int width) {
        this(context, types, type, width, 0);

    }

    public TypeWindow(Context context, String[] types, String type, int width, int layoutAdapterId) {
        super(context);
        mTypes = types;
        mWidth = width;
        mCurrentType = type;
        this.layoutId = layoutAdapterId;
    }

    @Override
    public void setUiBeforShow() {
        if (adapter == null) {
            adapter = new ListAdapter();
            mListview.setAdapter(adapter);
            ViewGroup.LayoutParams layoutParams = mListview.getLayoutParams();
            layoutParams.width = mWidth - 2;
            mListview.setLayoutParams(layoutParams);
            mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    mCurrentType = mTypes[position];
                    adapter.notifyDataSetChanged();
                    mOnItemClickListener.onItemClick(parent, view, position, id);
                }
            });
        }
    }

    @Override
    public View onCreatePopupView() {
        View inflate = View.inflate(mContext, R.layout.view_common_pop_list, null);
        mListview = inflate.findViewById(R.id.listview);
        return inflate;
    }

    public interface OnItemClickListener {
        void onItemClick(AdapterView<?> parent, View view, int position, long id);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    class ListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            if (mTypes != null && mTypes.length > 0)
                return mTypes.length;
            return 0;
        }

        @Override
        public Object getItem(int position) {
            if (mTypes != null && mTypes.length > 0)
                return null;
            return mTypes[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                if (layoutId != 0) {
                    convertView = View.inflate(mContext, layoutId, null);
                } else {
                    convertView = View.inflate(mContext, R.layout.list_item_common_pop_list, null);
                }
                holder = new ViewHolder();
                convertView.setTag(holder);
                holder.tvContent = convertView.findViewById(R.id.tv_content);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            String title = mTypes[position];
            holder.tvContent.setText(title);
            if (title.equals(mCurrentType)) {
                holder.tvContent.setSelected(true);
            } else {
                holder.tvContent.setSelected(false);
            }
            return convertView;
        }
    }

    static class ViewHolder {
        TextView tvContent;
    }
}
